package com.guang.syncronized.syncronizedwaitset;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @Description
 * @Author liguang
 * @Date 2022/10/23/13:04
 */
public class SyncronziedWaitSetTestTwo {
    private final static Logger logger = LoggerFactory.getLogger(SyncronziedWaitSetTestTwo.class);
    static Object lock = new Object();
    static volatile boolean flag = false;
    public static void main(String[] args) throws InterruptedException {

        new Thread(()->{
            synchronized (lock){
                while (!flag){
                    logger.info("因为条件不满足而释放锁");
                    try {
                        lock.wait();
                        logger.info("被唤醒了，再次检查是否满足条件");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                logger.info("条件满足，执行临界区代码");
            }
        },"tom").start();

        // 为了满足先进入到waitset集合中来
        TimeUnit.MICROSECONDS.sleep(10);
        synchronized (lock){
            for (int i = 0; i < 5; i++) {
                new Thread(()->{
                    synchronized (lock){
                        logger.info("当前当前名称是：{}",Thread.currentThread().getName());
                    }
                },"normal"+i).start();
                TimeUnit.MICROSECONDS.sleep(1);
            }
            flag = true;
            lock.notifyAll();
             logger.info("打印输出顺序");
        }
    }
}
